package frame.options.virtualNetworks.create;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import model.components.networks.factory.VirtualNetworksFactory;
import com.nexes.wizard.WizardPanelDescriptor;

public class CreateVirtualNetworksSelectDescriptor extends WizardPanelDescriptor implements DocumentListener, ActionListener
{
	public static final String IDENTIFIER = "CreateVirtualNetworksDescriptor";
    
	private CreateVirtualNetworksSelectPanel createVirtualNetworksPanel;
	public CreateVirtualNetworksSelectPanel getCreateVirtualNetworsPanel(){ return createVirtualNetworksPanel; }
	public void setCreateVirtualNetworsPanel( CreateVirtualNetworksSelectPanel createVirtualNetworksPanel ){ this.createVirtualNetworksPanel = createVirtualNetworksPanel; }
	
	private CreateVirtualNetworksWizard createVirtualNetworksWizard;
	public CreateVirtualNetworksWizard getCreateVirtualNetworksWizard(){ return createVirtualNetworksWizard; }
	public void setCreateVirtualNetworksWizard( CreateVirtualNetworksWizard createVirtualNetworksWizard ){ this.createVirtualNetworksWizard = createVirtualNetworksWizard; }
	
	public CreateVirtualNetworksSelectDescriptor()
    {
    	this.setCreateVirtualNetworsPanel( new CreateVirtualNetworksSelectPanel() );
    	this.getCreateVirtualNetworsPanel().addListeners( this, this );
        
    	this.setPanelDescriptorIdentifier( IDENTIFIER );
        this.setPanelComponent( this.getCreateVirtualNetworsPanel() );
    }
    
	@Override
	public Object getBackPanelDescriptor()
    {
        return null;
    }
	@Override
	public Object getNextPanelDescriptor()
    {
        return CreateVirtualNetworksFinishDescriptor.IDENTIFIER;
    }
	@Override
	public void aboutToDisplayPanel()
    {
    	this.setCreateVirtualNetworksWizard( (CreateVirtualNetworksWizard) this.getWizard() );
    	this.getCreateVirtualNetworksWizard().setNextFinishButtonEnabled( false );
    	this.enableFinishButton();
    }
	public void aboutToHidePanel()
	{
		this.createVirtualNetworks();
	}
	private void createVirtualNetworks()
	{
		CreateVirtualNetworksSelectPanel createVirtualNetworksPanel = this.getCreateVirtualNetworsPanel();
				
		int networksNumber = Integer.parseInt( createVirtualNetworksPanel.getNetworksNumberTextField().getText() );
		int windowTimeUnitsNumber = Integer.parseInt( createVirtualNetworksPanel.getWindowTimeUnitsNumberTextField().getText() );
		double poissonArrives = Double.parseDouble( createVirtualNetworksPanel.getPoissonArrivesTextField().getText() );
		double exponentialLifetime = Double.parseDouble( createVirtualNetworksPanel.getExponentialLifetimeTextField().getText() );
		
		int minNodesNumber = Integer.parseInt( createVirtualNetworksPanel.getMinNodesNumberTextField().getText() );
    	int maxNodesNumber = Integer.parseInt( createVirtualNetworksPanel.getMaxNodesNumberTextField().getText() );
    	
    	int routersPercentage  = Integer.parseInt( createVirtualNetworksPanel.getRoutersPercentageTextField().getText() );
    	int switchesPercentage = Integer.parseInt( createVirtualNetworksPanel.getSwitchesPercentageTextField().getText() );
    	int serversPercentage  = Integer.parseInt( createVirtualNetworksPanel.getServersPercentageTextField().getText() );
		
    	int linuxPercentage = Integer.parseInt( createVirtualNetworksPanel.getLinuxPercentageTextField().getText() );
		int windowsPercentage = Integer.parseInt( createVirtualNetworksPanel.getWindowsPercentageTextField().getText() );
		int solarisPercentage = Integer.parseInt( createVirtualNetworksPanel.getSolarisPercentageTextField().getText() );
		int androidPercentage = Integer.parseInt( createVirtualNetworksPanel.getAndroidPercentageTextField().getText() );
		
		int vmWarePercentage = Integer.parseInt( createVirtualNetworksPanel.getVmWarePercentageTextField().getText() );
		int xenPercentage = Integer.parseInt( createVirtualNetworksPanel.getXenPercentageTextField().getText() );
		int kvmPercentage = Integer.parseInt( createVirtualNetworksPanel.getKvmPercentageTextField().getText() );
		int umlPercentage = Integer.parseInt( createVirtualNetworksPanel.getUmlPercentageTextField().getText() );
		
		int minCpu = Integer.parseInt( createVirtualNetworksPanel.getMinCpuTextField().getText() );
		int maxCpu = Integer.parseInt( createVirtualNetworksPanel.getMaxCpuTextField().getText() );
		
		int minMemory = Integer.parseInt( createVirtualNetworksPanel.getMinMemoryTextField().getText() );
		int maxMemory = Integer.parseInt( createVirtualNetworksPanel.getMaxMemoryTextField().getText() );
		
		int minDiskSpace = Integer.parseInt( createVirtualNetworksPanel.getMinDiskSpaceTextField().getText() );
		int maxDiskSpace = Integer.parseInt( createVirtualNetworksPanel.getMaxDiskSpaceTextField().getText() );
		
    	int vlanPercentage  = Integer.parseInt( createVirtualNetworksPanel.getVlanPercentageTextField().getText() );
		int sonetPercentage = Integer.parseInt( createVirtualNetworksPanel.getSonetPercentageTextField().getText() );
		int eight0211Percentage = Integer.parseInt( createVirtualNetworksPanel.getEight0211PercentageTextField().getText() );
    	
		int minBandwidth = Integer.parseInt( createVirtualNetworksPanel.getMinBandwidthTextField().getText() );
		int maxBandwidth = Integer.parseInt( createVirtualNetworksPanel.getMaxBandwidthTextField().getText() );
		
		VirtualNetworksFactory virtualNetworksFactory = null;
		
		if( createVirtualNetworksPanel.getLinksNumberPercentageRadioButton().isSelected() )
		{
			int linksNumberPercentage = Integer.parseInt( createVirtualNetworksPanel.getLinksNumberPercentageTextField().getText() );
			virtualNetworksFactory = new VirtualNetworksFactory( VirtualNetworksFactory.NAME_PREFIX, networksNumber, windowTimeUnitsNumber, poissonArrives, exponentialLifetime, minNodesNumber, maxNodesNumber, routersPercentage, switchesPercentage, serversPercentage, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, vmWarePercentage, xenPercentage, kvmPercentage, umlPercentage, minCpu, maxCpu, minMemory, maxMemory, minDiskSpace, maxDiskSpace, linksNumberPercentage, vlanPercentage, sonetPercentage, eight0211Percentage, minBandwidth, maxBandwidth );
		}
		else if( createVirtualNetworksPanel.getLinksNumberRangeRadioButton().isSelected() )
		{
			int minLinksNumber = Integer.parseInt( createVirtualNetworksPanel.getMinLinksNumberTextField().getText() );
			int maxLinksNumber = Integer.parseInt( createVirtualNetworksPanel.getMaxLinksNumberTextField().getText() );
			virtualNetworksFactory = new VirtualNetworksFactory( VirtualNetworksFactory.NAME_PREFIX, networksNumber, windowTimeUnitsNumber, poissonArrives, exponentialLifetime, minNodesNumber, maxNodesNumber, routersPercentage, switchesPercentage, serversPercentage, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, vmWarePercentage, xenPercentage, kvmPercentage, umlPercentage, minCpu, maxCpu, minMemory, maxMemory, minDiskSpace, maxDiskSpace, minLinksNumber, maxLinksNumber, vlanPercentage, sonetPercentage, eight0211Percentage, minBandwidth, maxBandwidth );			
		}
		
		this.getCreateVirtualNetworksWizard().setVirtualNetworks( virtualNetworksFactory.getVirtualNetworks() );
	}
    
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		this.getCreateVirtualNetworsPanel().refreshLinksNumber();
		this.enableFinishButton();
	}
	@Override
    public void changedUpdate( DocumentEvent documentEvent )
    {
    	this.enableFinishButton();
    }
	@Override
	public void insertUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	@Override
	public void removeUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	
	private void enableFinishButton()
	{
		if( this.getCreateVirtualNetworsPanel().check() )
			this.getCreateVirtualNetworksWizard().setNextFinishButtonEnabled( true );
		else
        	this.getCreateVirtualNetworksWizard().setNextFinishButtonEnabled( false );
    }
	
}
